import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/model/pro_open_factor_model.dart';
import 'package:jinke/page/pro_list/subs/pro_open/main/index.dart';
import 'package:jinke/page/pro_list/subs/pro_open/subs/pro_open_step1/index.dart';
import 'package:jinke/utils/httputils/resultdata.dart';
import 'package:jinke/utils/util.dart';

import '../../../../../../utils/httputils/api.dart';
import 'index.dart';
import 'widgets/city_rows_delegate.dart';

class ProOpenStep2Controller extends GetxController {
  ProOpenStep2Controller();

  final state = ProOpenStep2State();

  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  void getFactors() async {
    var codeType = state.comMod?.enterpriseType;
    if (codeType == null) {
      return;
    }
    //ENTERPRISE_TYPE
    ResultData res =
        await ProListApi.createStep1(codeType: 'ELEMENTS_SECURITY');
    if (res.isSuccess) {
      List data = res.data;
      state.factors = data.map((e) => ProOpenFactorModel.fromJson(e)).toList();
      update();
    }
  }

  void step2Init() {
    final step1Ctrl = Get.find<ProOpenStep1Controller>();
    if (state.comMod?.enterpriseAlias == step1Ctrl.state.com?.enterpriseAlias) {
      return;
    }
    state.comMod = step1Ctrl.state.com;
    state.nameCtrl.text = state.comMod?.enterpriseName ?? '';
    state.briefCtrl.text = state.comMod?.enterpriseIntroduction ?? '';
    getFactors();
  }

  void setupSelectedFactors(List<int> indexs) {
    if (state.factors == null || state.factors?.isEmpty == true) {
      return;
    }
    state.selectedFactorls = [];
    for (var i = 0; i < state.factors!.length; i++) {
      if (indexs.contains(i)) {
        var item = state.factors![i];
        state.selectedFactorls?.add(item);
      }
    }
  }

  Future<bool> onNext() async {
    if (state.comFrom == null) {
      EasyLoading.showToast('请选择企业来源地');
      return false;
    }

    if (state.contacts == null || state.contacts?.isEmpty == true) {
      EasyLoading.showToast('请输入企业联系人');
      return false;
    }
    if (state.phone == null || state.phone?.isEmpty == true) {
      EasyLoading.showToast('请输入联系电话');
      return false;
    }
    if (state.selectedFactorls == null ||
        state.selectedFactorls?.isEmpty == true) {
      EasyLoading.showToast('请选择要素保障');
      return false;
    }

    return true;
  }

  void actionChooseCity() {
    List<Map<String, List>> list = [
      {
        '北京': [
          {'北京市'}
        ]
      },
      {
        '河南省': [
          {'郑州市'},
          {'洛阳市'}
        ]
      },
      {
        '河北省': [
          {'石家庄'},
          {'邯郸市'}
        ]
      },
      {
        '江苏省': [
          {'苏州市'},
          {'南京市'}
        ]
      }
    ];
    BrnMultiDataPicker(
      context: Get.context!,
      title: '企业来源地',
      delegate: Brn2RowDelegate(
          firstSelectedIndex: 1, secondSelectedIndex: 0, list: list),
      confirmClick: (indexs) {
        // BrnToast.show(list.toString(), Get.context!);
        var a = list[indexs.first];
        var b = a.values.first[indexs.last];
        state.comFrom = a.keys.first + b.first.toString();
        update();
      },
    ).show();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();

    final superCtrl = Get.find<ProOpenController>();
    superCtrl.state.stepIndexObs.listen((p0) {
      if (p0 == 1) {
        step2Init();
      }
    });
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
